package me.helllp.book.concurrent.ch03;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 重入锁的使用
 * 
 * @author Administrator
 *
 */
public class ReenterLockDemo implements Runnable{
	public static ReentrantLock lock = new ReentrantLock();
	public static int i = 0;
	
	@Override
	public void run() {
		for(int j=0; j<1000000; j++){
			lock.lock();
			try {
				i++;
			} finally {
				lock.unlock();
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		//	在一个线程中锁可以重复进入，故称为重入锁
		ReenterLockDemo obj = new ReenterLockDemo();
		
		Thread t1 = new Thread(obj);
		Thread t2 = new Thread(obj);
		t1.start();
		t2.start();
		t1.join();
		t2.join();
		System.out.println(i);
	}
}
